package com.cencat.driver.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cencat.common.result.Result;
import com.cencat.driver.entity.DeliveryTask;
import com.cencat.driver.service.DeliveryTaskService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 配送任务管理控制器
 * 
 * @author cencat
 * @since 2024-01-01
 */
@Slf4j
@RestController
@RequestMapping("/api/delivery-task")
@RequiredArgsConstructor
@Tag(name = "配送任务管理", description = "司机端系统 - 配送任务管理接口")
public class DeliveryTaskController {

    private final DeliveryTaskService deliveryTaskService;

    @GetMapping("/info/{id}")
    @Operation(summary = "获取配送任务信息", description = "根据任务ID获取配送任务详细信息")
    public Result<DeliveryTask> getTaskInfo(
            @Parameter(description = "任务ID", required = true) @PathVariable Long id,
            @Parameter(description = "租户ID", required = true) @RequestParam Long tenantId) {
        try {
            DeliveryTask task = deliveryTaskService.getById(id, tenantId);
            if (task == null) {
                return Result.error("配送任务不存在");
            }
            return Result.success(task, "获取配送任务信息成功");
        } catch (Exception e) {
            log.error("获取配送任务信息异常: id={}, tenantId={}", id, tenantId, e);
            return Result.error("获取配送任务信息失败: " + e.getMessage());
        }
    }

    @GetMapping("/info/code/{taskCode}")
    @Operation(summary = "根据任务编号获取配送任务信息", description = "根据任务编号获取配送任务详细信息")
    public Result<DeliveryTask> getTaskByCode(
            @Parameter(description = "任务编号", required = true) @PathVariable String taskCode,
            @Parameter(description = "租户ID", required = true) @RequestParam Long tenantId) {
        try {
            DeliveryTask task = deliveryTaskService.getByTaskCode(taskCode, tenantId);
            if (task == null) {
                return Result.error("配送任务不存在");
            }
            return Result.success(task, "获取配送任务信息成功");
        } catch (Exception e) {
            log.error("根据任务编号获取配送任务信息异常: taskCode={}, tenantId={}", taskCode, tenantId, e);
            return Result.error("获取配送任务信息失败: " + e.getMessage());
        }
    }

    @GetMapping("/driver/{driverId}")
    @Operation(summary = "获取司机任务列表", description = "根据司机ID获取其配送任务列表")
    public Result<List<DeliveryTask>> getTasksByDriverId(
            @Parameter(description = "司机ID", required = true) @PathVariable Long driverId,
            @Parameter(description = "任务状态") @RequestParam(required = false) Integer status,
            @Parameter(description = "租户ID", required = true) @RequestParam Long tenantId) {
        try {
            List<DeliveryTask> tasks = deliveryTaskService.getByDriverId(driverId, status, tenantId);
            return Result.success(tasks, "获取司机任务列表成功");
        } catch (Exception e) {
            log.error("获取司机任务列表异常: driverId={}, tenantId={}", driverId, tenantId, e);
            return Result.error("获取司机任务列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/order/{orderId}")
    @Operation(summary = "获取订单任务列表", description = "根据订单ID获取其配送任务列表")
    public Result<List<DeliveryTask>> getTasksByOrderId(
            @Parameter(description = "订单ID", required = true) @PathVariable Long orderId,
            @Parameter(description = "租户ID", required = true) @RequestParam Long tenantId) {
        try {
            List<DeliveryTask> tasks = deliveryTaskService.getByOrderId(orderId, tenantId);
            return Result.success(tasks, "获取订单任务列表成功");
        } catch (Exception e) {
            log.error("获取订单任务列表异常: orderId={}, tenantId={}", orderId, tenantId, e);
            return Result.error("获取订单任务列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/list")
    @Operation(summary = "获取配送任务列表", description = "分页获取配送任务列表，支持多条件筛选")
    public Result<IPage<DeliveryTask>> getTaskList(
            @Parameter(description = "当前页码") @RequestParam(defaultValue = "1") Long current,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Long size,
            @Parameter(description = "任务编号") @RequestParam(required = false) String taskCode,
            @Parameter(description = "司机ID") @RequestParam(required = false) Long driverId,
            @Parameter(description = "订单ID") @RequestParam(required = false) Long orderId,
            @Parameter(description = "任务状态") @RequestParam(required = false) Integer status,
            @Parameter(description = "任务类型") @RequestParam(required = false) Integer taskType,
            @Parameter(description = "优先级") @RequestParam(required = false) Integer priority,
            @Parameter(description = "开始时间") @RequestParam(required = false) LocalDateTime startTime,
            @Parameter(description = "结束时间") @RequestParam(required = false) LocalDateTime endTime,
            @Parameter(description = "租户ID", required = true) @RequestParam Long tenantId) {
        try {
            IPage<DeliveryTask> result = deliveryTaskService.getPageList(current, size, taskCode, driverId, orderId, status, taskType, priority, startTime, endTime, tenantId);
            return Result.success(result, "获取配送任务列表成功");
        } catch (Exception e) {
            log.error("获取配送任务列表异常: tenantId={}", tenantId, e);
            return Result.error("获取配送任务列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/pending")
    @Operation(summary = "获取待处理任务列表", description = "获取当前待处理的配送任务列表")
    public Result<List<DeliveryTask>> getPendingTasks(
            @Parameter(description = "司机ID") @RequestParam(required = false) Long driverId,
            @Parameter(description = "租户ID", required = true) @RequestParam Long tenantId) {
        try {
            List<DeliveryTask> result = deliveryTaskService.getPendingTasks(driverId, tenantId);
            return Result.success(result, "获取待处理任务列表成功");
        } catch (Exception e) {
            log.error("获取待处理任务列表异常: driverId={}, tenantId={}", driverId, tenantId, e);
            return Result.error("获取待处理任务列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/available")
    @Operation(summary = "获取可接任务列表", description = "获取当前可接的配送任务列表")
    public Result<List<DeliveryTask>> getAvailableTasks(
            @Parameter(description = "司机ID") @RequestParam(required = false) Long driverId,
            @Parameter(description = "租户ID", required = true) @RequestParam Long tenantId) {
        try {
            List<DeliveryTask> result = deliveryTaskService.getAvailableTasks(driverId, tenantId);
            return Result.success(result, "获取可接任务列表成功");
        } catch (Exception e) {
            log.error("获取可接任务列表异常: driverId={}, tenantId={}", driverId, tenantId, e);
            return Result.error("获取可接任务列表失败: " + e.getMessage());
        }
    }

    @PostMapping("/create")
    @Operation(summary = "创建配送任务", description = "创建新的配送任务")
    public Result<Boolean> createTask(
            @Parameter(description = "配送任务信息", required = true) @Valid @RequestBody DeliveryTask task) {
        try {
            boolean result = deliveryTaskService.createTask(task);
            return Result.success(result, "创建配送任务成功");
        } catch (Exception e) {
            log.error("创建配送任务异常: taskCode={}, orderId={}, tenantId={}", 
                     task.getTaskCode(), task.getOrderId(), task.getTenantId(), e);
            return Result.error("创建配送任务失败: " + e.getMessage());
        }
    }

    @PutMapping("/update")
    @Operation(summary = "更新配送任务信息", description = "更新配送任务基本信息")
    public Result<Boolean> updateTask(
            @Parameter(description = "配送任务信息", required = true) @Valid @RequestBody DeliveryTask task) {
        try {
            boolean result = deliveryTaskService.updateTask(task);
            return Result.success(result, "更新配送任务信息成功");
        } catch (Exception e) {
            log.error("更新配送任务信息异常: id={}, tenantId={}", task.getId(), task.getTenantId(), e);
            return Result.error("更新配送任务信息失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除配送任务", description = "删除指定配送任务")
    public Result<Boolean> deleteTask(
            @Parameter(description = "任务ID", required = true) @PathVariable Long id,
            @Parameter(description = "租户ID", required = true) @RequestParam Long tenantId) {
        try {
            boolean result = deliveryTaskService.deleteTask(id, tenantId);
            return Result.success(result, "删除配送任务成功");
        } catch (Exception e) {
            log.error("删除配送任务异常: id={}, tenantId={}", id, tenantId, e);
            return Result.error("删除配送任务失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/batch-delete")
    @Operation(summary = "批量删除配送任务", description = "批量删除多个配送任务")
    public Result<Boolean> batchDeleteTasks(
            @Parameter(description = "任务ID列表", required = true) @RequestBody List<Long> ids,
            @Parameter(description = "租户ID", required = true) @RequestParam Long tenantId) {
        try {
            boolean result = deliveryTaskService.batchDeleteTasks(ids, tenantId);
            return Result.success(result, "批量删除配送任务成功");
        } catch (Exception e) {
            log.error("批量删除配送任务异常: tenantId={}", tenantId, e);
            return Result.error("批量删除配送任务失败: " + e.getMessage());
        }
    }

    @PutMapping("/status/{id}")
    @Operation(summary = "更新任务状态", description = "更新配送任务的状态")
    public Result<Boolean> updateTaskStatus(
            @Parameter(description = "任务ID", required = true) @PathVariable Long id,
            @Parameter(description = "状态", required = true) @RequestParam Integer status,
            @Parameter(description = "备注") @RequestParam(required = false) String remark,
            @Parameter(description = "租户ID", required = true) @RequestParam Long tenantId) {
        try {
            boolean result = deliveryTaskService.updateStatus(id, status, remark, tenantId);
            return Result.success(result, "更新任务状态成功");
        } catch (Exception e) {
            log.error("更新任务状态异常: id={}, status={}, tenantId={}", id, status, tenantId, e);
            return Result.error("更新任务状态失败: " + e.getMessage());
        }
    }

    @PostMapping("/assign-driver")
    @Operation(summary = "分配司机", description = "为配送任务分配司机")
    public Result<Boolean> assignDriver(
            @Parameter(description = "任务ID", required = true) @RequestParam Long taskId,
            @Parameter(description = "司机ID", required = true) @RequestParam Long driverId,
            @Parameter(description = "租户ID", required = true) @RequestParam Long tenantId) {
        try {
            boolean result = deliveryTaskService.assignDriver(taskId, driverId, tenantId);
            return Result.success(result, "分配司机成功");
        } catch (Exception e) {
            log.error("分配司机异常: taskId={}, driverId={}, tenantId={}", taskId, driverId, tenantId, e);
            return Result.error("分配司机失败: " + e.getMessage());
        }
    }

    @PostMapping("/unassign-driver")
    @Operation(summary = "取消分配司机", description = "取消配送任务的司机分配")
    public Result<Boolean> unassignDriver(
            @Parameter(description = "任务ID", required = true) @RequestParam Long taskId,
            @Parameter(description = "租户ID", required = true) @RequestParam Long tenantId) {
        try {
            boolean result = deliveryTaskService.unassignDriver(taskId, tenantId);
            return Result.success(result, "取消分配司机成功");
        } catch (Exception e) {
            log.error("取消分配司机异常: taskId={}, tenantId={}", taskId, tenantId, e);
            return Result.error("取消分配司机失败: " + e.getMessage());
        }
    }

    @PostMapping("/accept/{id}")
    @Operation(summary = "接受任务", description = "司机接受配送任务")
    public Result<Boolean> acceptTask(
            @Parameter(description = "任务ID", required = true) @PathVariable Long id,
            @Parameter(description = "司机ID", required = true) @RequestParam Long driverId,
            @Parameter(description = "租户ID", required = true) @RequestParam Long tenantId) {
        try {
            boolean result = deliveryTaskService.acceptTask(id, driverId, tenantId);
            return Result.success(result, "接受任务成功");
        } catch (Exception e) {
            log.error("接受任务异常: id={}, driverId={}, tenantId={}", id, driverId, tenantId, e);
            return Result.error("接受任务失败: " + e.getMessage());
        }
    }

    @PostMapping("/reject/{id}")
    @Operation(summary = "拒绝任务", description = "司机拒绝配送任务")
    public Result<Boolean> rejectTask(
            @Parameter(description = "任务ID", required = true) @PathVariable Long id,
            @Parameter(description = "司机ID", required = true) @RequestParam Long driverId,
            @Parameter(description = "拒绝原因") @RequestParam(required = false) String reason,
            @Parameter(description = "租户ID", required = true) @RequestParam Long tenantId) {
        try {
            boolean result = deliveryTaskService.rejectTask(id, driverId, reason, tenantId);
            return Result.success(result, "拒绝任务成功");
        } catch (Exception e) {
            log.error("拒绝任务异常: id={}, driverId={}, tenantId={}", id, driverId, tenantId, e);
            return Result.error("拒绝任务失败: " + e.getMessage());
        }
    }

    @PostMapping("/start/{id}")
    @Operation(summary = "开始任务", description = "司机开始执行配送任务")
    public Result<Boolean> startTask(
            @Parameter(description = "任务ID", required = true) @PathVariable Long id,
            @Parameter(description = "司机ID", required = true) @RequestParam Long driverId,
            @Parameter(description = "开始位置经度") @RequestParam(required = false) Double longitude,
            @Parameter(description = "开始位置纬度") @RequestParam(required = false) Double latitude,
            @Parameter(description = "租户ID", required = true) @RequestParam Long tenantId) {
        try {
            boolean result = deliveryTaskService.startTask(id, driverId, longitude, latitude, tenantId);
            return Result.success(result, "开始任务成功");
        } catch (Exception e) {
            log.error("开始任务异常: id={}, driverId={}, tenantId={}", id, driverId, tenantId, e);
            return Result.error("开始任务失败: " + e.getMessage());
        }
    }

    @PostMapping("/complete/{id}")
    @Operation(summary = "完成任务", description = "司机完成配送任务")
    public Result<Boolean> completeTask(
            @Parameter(description = "任务ID", required = true) @PathVariable Long id,
            @Parameter(description = "司机ID", required = true) @RequestParam Long driverId,
            @Parameter(description = "完成位置经度") @RequestParam(required = false) Double longitude,
            @Parameter(description = "完成位置纬度") @RequestParam(required = false) Double latitude,
            @Parameter(description = "完成备注") @RequestParam(required = false) String remark,
            @Parameter(description = "租户ID", required = true) @RequestParam Long tenantId) {
        try {
            boolean result = deliveryTaskService.completeTask(id, driverId, longitude, latitude, remark, tenantId);
            return Result.success(result, "完成任务成功");
        } catch (Exception e) {
            log.error("完成任务异常: id={}, driverId={}, tenantId={}", id, driverId, tenantId, e);
            return Result.error("完成任务失败: " + e.getMessage());
        }
    }

    @PostMapping("/cancel/{id}")
    @Operation(summary = "取消任务", description = "取消配送任务")
    public Result<Boolean> cancelTask(
            @Parameter(description = "任务ID", required = true) @PathVariable Long id,
            @Parameter(description = "取消原因", required = true) @RequestParam String reason,
            @Parameter(description = "租户ID", required = true) @RequestParam Long tenantId) {
        try {
            boolean result = deliveryTaskService.cancelTask(id, reason, tenantId);
            return Result.success(result, "取消任务成功");
        } catch (Exception e) {
            log.error("取消任务异常: id={}, tenantId={}", id, tenantId, e);
            return Result.error("取消任务失败: " + e.getMessage());
        }
    }

    @GetMapping("/timeout")
    @Operation(summary = "获取超时任务列表", description = "获取超时的配送任务列表")
    public Result<List<DeliveryTask>> getTimeoutTasks(
            @Parameter(description = "租户ID", required = true) @RequestParam Long tenantId) {
        try {
            List<DeliveryTask> result = deliveryTaskService.getTimeoutTasks(tenantId);
            return Result.success(result, "获取超时任务列表成功");
        } catch (Exception e) {
            log.error("获取超时任务列表异常: tenantId={}", tenantId, e);
            return Result.error("获取超时任务列表失败: " + e.getMessage());
        }
    }

    @PostMapping("/handle-timeout")
    @Operation(summary = "处理超时任务", description = "处理超时的配送任务")
    public Result<Map<String, Object>> handleTimeoutTasks(
            @Parameter(description = "租户ID", required = true) @RequestParam Long tenantId) {
        try {
            Map<String, Object> result = deliveryTaskService.handleTimeoutTasks(tenantId);
            return Result.success(result, "处理超时任务完成");
        } catch (Exception e) {
            log.error("处理超时任务异常: tenantId={}", tenantId, e);
            return Result.error("处理超时任务失败: " + e.getMessage());
        }
    }

    @PostMapping("/smart-dispatch")
    @Operation(summary = "智能派单", description = "基于算法进行智能派单")
    public Result<Map<String, Object>> smartDispatch(
            @Parameter(description = "任务ID列表", required = true) @RequestBody List<Long> taskIds,
            @Parameter(description = "租户ID", required = true) @RequestParam Long tenantId) {
        try {
            Map<String, Object> result = deliveryTaskService.smartDispatch(taskIds, tenantId);
            return Result.success(result, "智能派单完成");
        } catch (Exception e) {
            log.error("智能派单异常: tenantId={}", tenantId, e);
            return Result.error("智能派单失败: " + e.getMessage());
        }
    }

    @GetMapping("/statistics")
    @Operation(summary = "获取任务统计信息", description = "获取配送任务相关的统计数据")
    public Result<Map<String, Object>> getStatistics(
            @Parameter(description = "司机ID") @RequestParam(required = false) Long driverId,
            @Parameter(description = "开始日期") @RequestParam(required = false) String startDate,
            @Parameter(description = "结束日期") @RequestParam(required = false) String endDate,
            @Parameter(description = "租户ID", required = true) @RequestParam Long tenantId) {
        try {
            Map<String, Object> result = deliveryTaskService.getStatistics(driverId, startDate, endDate, tenantId);
            return Result.success(result, "获取任务统计信息成功");
        } catch (Exception e) {
            log.error("获取任务统计信息异常: driverId={}, tenantId={}", driverId, tenantId, e);
            return Result.error("获取任务统计信息失败: " + e.getMessage());
        }
    }

    @GetMapping("/performance/{driverId}")
    @Operation(summary = "获取司机任务绩效", description = "获取指定司机的任务绩效数据")
    public Result<Map<String, Object>> getDriverTaskPerformance(
            @Parameter(description = "司机ID", required = true) @PathVariable Long driverId,
            @Parameter(description = "开始日期") @RequestParam(required = false) String startDate,
            @Parameter(description = "结束日期") @RequestParam(required = false) String endDate,
            @Parameter(description = "租户ID", required = true) @RequestParam Long tenantId) {
        try {
            Map<String, Object> result = deliveryTaskService.getDriverTaskPerformance(driverId, startDate, endDate, tenantId);
            return Result.success(result, "获取司机任务绩效成功");
        } catch (Exception e) {
            log.error("获取司机任务绩效异常: driverId={}, tenantId={}", driverId, tenantId, e);
            return Result.error("获取司机任务绩效失败: " + e.getMessage());
        }
    }

    @GetMapping("/export")
    @Operation(summary = "导出任务信息", description = "导出配送任务信息到Excel")
    public Result<List<Map<String, Object>>> exportTasks(
            @Parameter(description = "任务编号") @RequestParam(required = false) String taskCode,
            @Parameter(description = "司机ID") @RequestParam(required = false) Long driverId,
            @Parameter(description = "订单ID") @RequestParam(required = false) Long orderId,
            @Parameter(description = "任务状态") @RequestParam(required = false) Integer status,
            @Parameter(description = "任务类型") @RequestParam(required = false) Integer taskType,
            @Parameter(description = "开始时间") @RequestParam(required = false) LocalDateTime startTime,
            @Parameter(description = "结束时间") @RequestParam(required = false) LocalDateTime endTime,
            @Parameter(description = "租户ID", required = true) @RequestParam Long tenantId) {
        try {
            List<Map<String, Object>> result = deliveryTaskService.exportTasks(taskCode, driverId, orderId, status, taskType, startTime, endTime, tenantId);
            return Result.success(result, "导出任务信息成功");
        } catch (Exception e) {
            log.error("导出任务信息异常: tenantId={}", tenantId, e);
            return Result.error("导出任务信息失败: " + e.getMessage());
        }
    }

    @PostMapping("/import")
    @Operation(summary = "导入任务信息", description = "从Excel导入配送任务信息")
    public Result<Map<String, Object>> importTasks(
            @Parameter(description = "任务数据列表", required = true) @RequestBody List<Map<String, Object>> taskList,
            @Parameter(description = "租户ID", required = true) @RequestParam Long tenantId) {
        try {
            Map<String, Object> result = deliveryTaskService.importTasks(taskList, tenantId);
            return Result.success(result, "导入任务信息完成");
        } catch (Exception e) {
            log.error("导入任务信息异常: tenantId={}", tenantId, e);
            return Result.error("导入任务信息失败: " + e.getMessage());
        }
    }

    @PostMapping("/batch-assign")
    @Operation(summary = "批量分配任务", description = "批量为司机分配配送任务")
    public Result<Map<String, Object>> batchAssignTasks(
            @Parameter(description = "任务分配列表", required = true) @RequestBody List<Map<String, Object>> assignList,
            @Parameter(description = "租户ID", required = true) @RequestParam Long tenantId) {
        try {
            Map<String, Object> result = deliveryTaskService.batchAssignTasks(assignList, tenantId);
            return Result.success(result, "批量分配任务完成");
        } catch (Exception e) {
            log.error("批量分配任务异常: tenantId={}", tenantId, e);
            return Result.error("批量分配任务失败: " + e.getMessage());
        }
    }

    @PostMapping("/batch-update-status")
    @Operation(summary = "批量更新任务状态", description = "批量更新多个配送任务的状态")
    public Result<Boolean> batchUpdateStatus(
            @Parameter(description = "任务ID列表", required = true) @RequestBody List<Long> ids,
            @Parameter(description = "状态", required = true) @RequestParam Integer status,
            @Parameter(description = "备注") @RequestParam(required = false) String remark,
            @Parameter(description = "租户ID", required = true) @RequestParam Long tenantId) {
        try {
            boolean result = deliveryTaskService.batchUpdateStatus(ids, status, remark, tenantId);
            return Result.success(result, "批量更新任务状态成功");
        } catch (Exception e) {
            log.error("批量更新任务状态异常: status={}, tenantId={}", status, tenantId, e);
            return Result.error("批量更新任务状态失败: " + e.getMessage());
        }
    }

    @GetMapping("/track/{id}")
    @Operation(summary = "获取任务轨迹", description = "获取指定配送任务的执行轨迹")
    public Result<List<Map<String, Object>>> getTaskTrack(
            @Parameter(description = "任务ID", required = true) @PathVariable Long id,
            @Parameter(description = "租户ID", required = true) @RequestParam Long tenantId) {
        try {
            List<Map<String, Object>> result = deliveryTaskService.getTaskTrack(id, tenantId);
            return Result.success(result, "获取任务轨迹成功");
        } catch (Exception e) {
            log.error("获取任务轨迹异常: id={}, tenantId={}", id, tenantId, e);
            return Result.error("获取任务轨迹失败: " + e.getMessage());
        }
    }

    @PostMapping("/update-location/{id}")
    @Operation(summary = "更新任务位置", description = "更新配送任务的实时位置信息")
    public Result<Boolean> updateTaskLocation(
            @Parameter(description = "任务ID", required = true) @PathVariable Long id,
            @Parameter(description = "经度", required = true) @RequestParam Double longitude,
            @Parameter(description = "纬度", required = true) @RequestParam Double latitude,
            @Parameter(description = "地址") @RequestParam(required = false) String address,
            @Parameter(description = "租户ID", required = true) @RequestParam Long tenantId) {
        try {
            boolean result = deliveryTaskService.updateTaskLocation(id, longitude, latitude, address, tenantId);
            return Result.success(result, "更新任务位置成功");
        } catch (Exception e) {
            log.error("更新任务位置异常: id={}, longitude={}, latitude={}, tenantId={}", 
                     id, longitude, latitude, tenantId, e);
            return Result.error("更新任务位置失败: " + e.getMessage());
        }
    }

    @GetMapping("/nearby")
    @Operation(summary = "获取附近任务", description = "根据位置获取附近的配送任务")
    public Result<List<Map<String, Object>>> getNearbyTasks(
            @Parameter(description = "经度", required = true) @RequestParam Double longitude,
            @Parameter(description = "纬度", required = true) @RequestParam Double latitude,
            @Parameter(description = "搜索半径(公里)") @RequestParam(defaultValue = "5") Double radius,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "10") Integer limit,
            @Parameter(description = "租户ID", required = true) @RequestParam Long tenantId) {
        try {
            List<Map<String, Object>> result = deliveryTaskService.getNearbyTasks(longitude, latitude, radius, limit, tenantId);
            return Result.success(result, "获取附近任务成功");
        } catch (Exception e) {
            log.error("获取附近任务异常: longitude={}, latitude={}, tenantId={}", longitude, latitude, tenantId, e);
            return Result.error("获取附近任务失败: " + e.getMessage());
        }
    }

    @PostMapping("/feedback/{id}")
    @Operation(summary = "任务反馈", description = "对配送任务进行反馈")
    public Result<Boolean> taskFeedback(
            @Parameter(description = "任务ID", required = true) @PathVariable Long id,
            @Parameter(description = "反馈类型", required = true) @RequestParam String feedbackType,
            @Parameter(description = "反馈内容", required = true) @RequestParam String content,
            @Parameter(description = "反馈人ID") @RequestParam(required = false) Long feedbackUserId,
            @Parameter(description = "租户ID", required = true) @RequestParam Long tenantId) {
        try {
            boolean result = deliveryTaskService.taskFeedback(id, feedbackType, content, feedbackUserId, tenantId);
            return Result.success(result, "任务反馈成功");
        } catch (Exception e) {
            log.error("任务反馈异常: id={}, feedbackType={}, tenantId={}", id, feedbackType, tenantId, e);
            return Result.error("任务反馈失败: " + e.getMessage());
        }
    }

    @GetMapping("/feedback/{id}")
    @Operation(summary = "获取任务反馈", description = "获取指定配送任务的反馈信息")
    public Result<List<Map<String, Object>>> getTaskFeedback(
            @Parameter(description = "任务ID", required = true) @PathVariable Long id,
            @Parameter(description = "租户ID", required = true) @RequestParam Long tenantId) {
        try {
            List<Map<String, Object>> result = deliveryTaskService.getTaskFeedback(id, tenantId);
            return Result.success(result, "获取任务反馈成功");
        } catch (Exception e) {
            log.error("获取任务反馈异常: id={}, tenantId={}", id, tenantId, e);
            return Result.error("获取任务反馈失败: " + e.getMessage());
        }
    }

    @GetMapping("/ranking")
    @Operation(summary = "获取任务排行榜", description = "获取配送任务相关的排行榜")
    public Result<List<Map<String, Object>>> getTaskRanking(
            @Parameter(description = "排行类型") @RequestParam(defaultValue = "completion_count") String rankType,
            @Parameter(description = "开始日期") @RequestParam(required = false) String startDate,
            @Parameter(description = "结束日期") @RequestParam(required = false) String endDate,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "10") Integer limit,
            @Parameter(description = "租户ID", required = true) @RequestParam Long tenantId) {
        try {
            List<Map<String, Object>> result = deliveryTaskService.getTaskRanking(rankType, startDate, endDate, limit, tenantId);
            return Result.success(result, "获取任务排行榜成功");
        } catch (Exception e) {
            log.error("获取任务排行榜异常: rankType={}, tenantId={}", rankType, tenantId, e);
            return Result.error("获取任务排行榜失败: " + e.getMessage());
        }
    }
}